package com.nineclock.system.service.impl;

import afu.org.checkerframework.checker.units.qual.A;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.filter.CurrentUserHolder;
import com.nineclock.common.utils.BeanHelper;
import com.nineclock.system.dto.IdAndGradesDTO;
import com.nineclock.system.dto.SysCompanyUserPayingDTO;
import com.nineclock.system.dto.SysPayGradesDTO;
import com.nineclock.system.mapper.SysCompanyUserPayingMapper;
import com.nineclock.system.mapper.SysPayGradesMapper;
import com.nineclock.system.pojo.SysCompanyUser;
import com.nineclock.system.pojo.SysCompanyUserPaying;
import com.nineclock.system.pojo.SysPayGrades;
import com.nineclock.system.service.SysCompanyUserPayingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.auditing.CurrentDateTimeProvider;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;


/**
 * @Author: Xu
 * @Date: 2021/12/04/21:13
 */
@Service
@Transactional
public class SysCompanyUserPayingImpl implements SysCompanyUserPayingService {

    @Autowired
    private SysCompanyUserPayingMapper companyUserPayingMapper;
    @Autowired
    private SysPayGradesMapper payGradesMapper;
    @Override
    public List<SysCompanyUserPayingDTO> allPaying() {
        Long companyId = CurrentUserHolder.get().getCompanyId();
        LambdaQueryWrapper<SysCompanyUserPaying> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysCompanyUserPaying::getCompanyId,companyId);
        List<SysCompanyUserPaying> sysCompanyUserPayings = companyUserPayingMapper.selectList(wrapper);
        if (!CollectionUtils.isEmpty(sysCompanyUserPayings)){
            List<SysCompanyUserPayingDTO> sysCompanyUserPayingDTOS = BeanHelper.copyWithCollection(sysCompanyUserPayings, SysCompanyUserPayingDTO.class);

            return  sysCompanyUserPayingDTOS;

        }
        return  null;

    }

    /**
     * 获取薪资等级列表
     * @return
     */
    @Override
    public List<SysPayGradesDTO> findPayList() {
        List<SysPayGrades> payList = payGradesMapper.findPayList();
        List<SysPayGradesDTO> sysPayGradesDTOS = BeanHelper.copyWithCollection(payList, SysPayGradesDTO.class);
        return sysPayGradesDTOS;
    }

    /**
     * 更新员工的薪资等级
     * @param
     * @param
     */
    @Override
    public void updateGrades(Map<String,String> map ){


        Long id = Long.valueOf(map.get("id"));
        String payGrades = map.get("payGrades");

        if (StringUtils.isEmpty(id) || StringUtils.isEmpty(payGrades)){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }
        //根据id 以及  等级列表  更新 sysCompanyUserPay表
        LambdaQueryWrapper<SysPayGrades> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysPayGrades::getPayGrades,payGrades);
        SysPayGrades sysPayGrades = payGradesMapper.selectOne(wrapper);
        //获取等级薪资对应的金额
        Long paying = sysPayGrades.getPaying();

        SysCompanyUserPaying sysCompanyUserPaying = new SysCompanyUserPaying();
        sysCompanyUserPaying.setPayGrades(payGrades);  //set Grades: P1
        sysCompanyUserPaying.setPaying(paying);  //set paying  50000

        UpdateWrapper<SysCompanyUserPaying> UpdateWrapper = new UpdateWrapper<>();
        UpdateWrapper.eq("id",id);  // id = 1
        int update = companyUserPayingMapper.update(sysCompanyUserPaying, UpdateWrapper);

    }
}
